<template>
  <div class="backPassword">
    <div class="van-nav-bar">
      <div class="van-nav-bar-left"
           @click="back ">
        <van-icon size="16"
                  name="arrow-left"
                  color="#FFF" />返回
      </div>
      <div class="van-nav-bar-title">找回密码</div>
    </div>

    <div class="banner">
      <img class="auto-img"
           src="../assets/ruixing_a.png"
           alt="">
    </div>

    <div class="login-box">
      <van-form>
        <van-field v-model="phone"
                   name="手机号"
                   label="手机号"
                   placeholder="请输入手机号"
                   autocomplete="off" />
        <van-field v-model="password"
                   :type="!viewPassword ? 'password' : 'text'"
                   :right-icon="!viewPassword ? 'closed-eye' : 'eye-o'"
                   @click-right-icon="getviewPassword"
                   name="新密码"
                   label="新密码"
                   autocomplete="off"
                   placeholder="请输入新密码" />

        <div style="margin: 16px;">
          <div class="login-item">
            <van-button round
                        block
                        color="#0C34BA"
                        @click="getRetrievePassword">提交</van-button>
          </div>
        </div>
      </van-form>
    </div>
  </div>
</template>

<script>
export default {
  name: 'BackPassword',
  data () {
    return {
      // 注册时的手机号
      phone: '',
      // 新密码
      password: '',
      // 找回密码可见状态
      viewPassword: false
    };
  },
  methods: {

    //切换找回密码可见状态
    getviewPassword () {
      this.viewPassword = !this.viewPassword
    },

    // 返回
    back () {
      this.$router.go(-1)
    },

    //  找回密码
    getRetrievePassword () {
      if (this.phone == '' || this.password == '') {
        this.$toast('手机号和新密码不能为空!')
        return
      }
      this.axios({
        method: 'post',
        url: '/retrievePassword',
        data: {
          phone: this.phone,
          password: this.password
        }
      }).then(result => {
        if (result.data.code == "L001") {
          const toast = this.$toast.loading({
            duration: 0, // 持续展示 toast
            forbidClick: true,
            message: '找回密码成功',
          });

          let second = 2;
          const timer = setInterval(() => {
            second--;
            if (second) {
              toast.message = `正在回到登录页面...`;

            } else {
              clearInterval(timer);
              // 手动清除 Toast
              this.$toast.clear();
              this.$router.push({ name: 'Login' })
            }
          }, 1000);


        }

        if (result.data.code == "L003") {
          this.$toast('新密码不能和原密码一致!')
        }
      })
    },

  }

}
</script>

<style lang="scss" scoped>
.backPassword {
  .van-nav-bar {
    padding-top: 30px;
    padding-bottom: 10px;
    display: flex;
    align-items: center;
    background-color: #0c34ba;
    color: #fff;

    .van-nav-bar-title {
      margin-left: 115px;
      font-size: 16px;
    }
    .van-nav-bar-left {
      font-size: 16px;
    }
  }
}
</style>